<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="./assets/mui/css/mui.min.css">
    <link rel="stylesheet" href="css/base.css">
    <link rel="stylesheet" href="assets/fontAwesome/css/font-awesome.min.css">
    <script src="./assets/mui/js/mui.min.js"></script>
    <title>Document</title>
    <style>
        .getCode{
            position: absolute;
            top: 50%;
            right: 10px;
            transform: translateY(-50%)
        }
        .mui-content,
        .mui-content input{
            font-size: 14px;
        }
        .login-now{
            float: right;
        }
    </style>
</head>
<body>
    <!-- 头部 -->
	<header class="mui-bar mui-bar-nav my-header">
		<h1 class="mui-title">乐淘云购</h1>
    </header>
    <!-- 底部 -->
	<nav class="mui-bar mui-bar-tab my-footer">
		<a class="mui-tab-item mui-active" href="index.html">
			<span class="mui-icon mui-icon-home"></span>
			<span class="mui-tab-label">首页</span>
		</a>
		<a class="mui-tab-item" href="category.html">
			<span class="mui-icon mui-icon-bars"></span>
			<span class="mui-tab-label">分类</span>
		</a>
		<a class="mui-tab-item" href="cart.html">
			<span class="mui-icon fa fa-shopping-cart"></span>
			<span class="mui-tab-label">购物车</span>
		</a>
		<a class="mui-tab-item" href="user.html">
			<span class="mui-icon mui-icon-person"></span>
			<span class="mui-tab-label">会员中心</span>
		</a>
    </nav>
    
	<div class="mui-content">
        <form class="mui-input-group">
            <div class="mui-input-row">
                <label>用户名</label>
            <input name="username" id="username" type="text" class="mui-input-clear" placeholder="请输入用户名">
            </div>
            <div class="mui-input-row">
                    <label>手机号</label>
                <input name="phone" id="phone" type="text" class="mui-input-clear" placeholder="请输入用户名">
                </div>
            <div class="mui-input-row">
                <label>密码</label>
                <input name="password" id="password" type="password" class="mui-input-password" placeholder="请输入密码">
            </div>
            <div class="mui-input-row">
                <label>确认密码</label>
                <input name="repassword" id="repassword" type="password" class="mui-input-password" placeholder="请输入密码">
            </div>
            <div class="mui-input-row">
                <label>认证码</label>
                <input type="text" class="mui-input-clear" placeholder="认证码" name="code">
                <a href="javascript:;" class="getCode" id="getCode">获取认证码</a>
            </div>
            <div class="mui-button-row">
                <button type="button" class="mui-btn mui-btn-primary" id="register-btn">注册</button>
            </div>
        </form>
        <a href="login.html" class="login-now">立即登录</a>
	</div>
</body>
<script src="assets/zepto/zepto.min.js"></script>
<script>
    $(function(){
        //鼠标失去焦点事件
        $("#username").on("blur",function(){
            //获取用户输入表单的内容
            var username = $("[name='username']").val();
            //判断验证用户输入是否正确
            if(username.trim() == ""){
                mui.toast('用户名不能为空');
            }
        });
        $("#phone").on("blur",function(){
            //获取用户输入表单的内容
            var phone = $("[name='phone']").val();
            var reg =/^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/;
            if(phone.trim() == ""){
                mui.toast('手机号码不能为空');
            }else if(!reg.test(phone)){
                mui.toast('手机号码格式不正确');
            }
        });
        $("#getCode").on("blur",function(){
            //获取用户输入表单的内容
            var code = $("[name='code']").val();
            //判断验证用户输入是否正确
            if(code.trim() == ""){
                mui.toast('验证码不能为空');
            }
        });
        $("#password").on("blur",function(){
            //获取用户输入表单的内容
            var password = $("[name='password']").val();
            //判断验证用户输入是否正确
            if(password.trim() == ""){
                mui.toast('密码 不能为空');
            }
        });
        $("#repassword").on("blur",function(){
            //获取用户输入表单的内容
            var repassword = $("[name='repassword']").val();
            var password = $("[name='password']").val();
            //判断验证用户输入是否正确
            if(repassword.trim() == ""){
                mui.toast('密码不能为空');
            }else if( repassword != password){
                mui.toast('两次密码不一致');
            }
        });
        //获取验证码
        $("#getCode").on("click",function(){
            $.ajax({
                type:"get",
                url:"/user/vCode",
                success:function(res){
                    console.log(res);
                }
            });
        });
        $("#register-btn").on("click",function(){
            //获取用户输入表单的内容
            var username = $("[name='username']").val();
            var phone = $("[name='phone']").val();
            var password = $("[name='password']").val();
            var repassword = $("[name='repassword']").val();
            var code = $("[name='code']").val();
            console.log(code);
            //根据用户输入数据向后台发送请求,验证数据
            $.ajax({
                type:"post",
                url:"/user/register",
                data:{
                    "username":username,
                    "password":password,
                    "mobile":phone,
                    "vCode":code
                },
                success:function(res){
                    //判断是否注册成功
                    if(res.success){
                        setTimeout(() => {
                            location.href = "login.html";
                        }, 1000);
                    }else{
                        mui.toast(res.message);
                    }
                    
                }
            });
            
        });

        //获取验证码
        $(".code").on("click",function(){
            $.ajax({
                type:"get",
                url:"/user/vCode",
                dataType:"json",
                success:function(res){
                    console.log(res);
                }

            });
        });
    })
    
    
</script>
</html>